package top.didi.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.didi.study.base.PageBean;
import top.didi.study.base.R;
import top.didi.study.dao.TbBillBoardDAO;
import top.didi.study.entity.TbBillBoard;
import top.didi.study.service.TbBillBoardService;
import top.didi.study.vo.PageVO;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbBillBoardServiceImpl implements TbBillBoardService {
    private final TbBillBoardDAO tbBillBoardDAO;

    @Override
    public R<String> insert(TbBillBoard tbBillBoard) {
        int i = tbBillBoardDAO.insert(tbBillBoard);
        return i == 1 ? R.ok("添加成功", null) : R.fail("添加失败", null);
    }

    @Override
    public R<String> update(TbBillBoard tbBillBoard) {
        int i = tbBillBoardDAO.updateById(tbBillBoard);
        return i == 1 ? R.ok("修改成功", null) : R.fail("修改失败", null);
    }

    @Override
    public R<String> delete(Integer id) {
        int i = tbBillBoardDAO.deleteById(id);
        return i == 1 ? R.ok("删除成功", null) : R.fail("删除失败", null);
    }

    @Override
    public R<TbBillBoard> queryById(Integer id) {
        TbBillBoard tbBillBoard = tbBillBoardDAO.selectById(id);
        return tbBillBoard != null ? R.ok(tbBillBoard) : R.fail("查询失败", null);
    }

    @Override
    public R<PageVO<TbBillBoard>> queryPage(PageBean pageBean, TbBillBoard tbBillBoard) {
        IPage<TbBillBoard> page = new Page<>(pageBean.getPageNumber(),pageBean.getPageSize());
        QueryWrapper<TbBillBoard> wrapper = new QueryWrapper<>();
        wrapper.like(
                StringUtils.hasText(tbBillBoard.getTitle()),
                "title",
                tbBillBoard.getTitle()
        );
        wrapper.like(
                StringUtils.hasText(tbBillBoard.getContent()),
                "content",
                tbBillBoard.getContent()
        );
        page = tbBillBoardDAO.selectPage(page,wrapper);
        return R.ok(page);
    }
}
